package laMalinche.testingPlayGround.swingTest;

import laMalinche.testingPlayGround.flow.CollectorIfc;
import laMalinche.testingPlayGround.flow.FlowHolder;
import laMalinche.testingPlayGround.testModel.PersonVo;
import laMalinche.constraint.ConstraintResult;
import laMalinche.constraint.ConstraintRuleListener;
import laMalinche.rete.rule.BusinessRuleType;
import laMalinche.rete.rule.ProductionRule;
import laMalinche.LaMalinche;
import laMalinche.ruleTailing.BusinessRelevantPropertyListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import static java.awt.Component.LEFT_ALIGNMENT;
import java.util.Set;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ran_tene
 * Date: Sep 3, 2008
 * Time: 11:32:02 AM
 * To change this template use File | Settings | File Templates.
 */
public class FamilyEditor implements ActionListener, ConstraintRuleListener, BusinessRelevantPropertyListener {
    private JFrame frame;
    private PersonVo flowPerson;
    private PropertyEditor propertyEditor;
    private ConstraintResult constraintResult;
    private LaMalinche laMalinche;
    private FlowHolder swingFlow;


    public FamilyEditor(LaMalinche laMalinche, FlowHolder swingFlow) {
        this.laMalinche = laMalinche;
        laMalinche.setConstraintRuleListener(this);
        laMalinche.addRelevantPropertyListener(this);
        this.swingFlow = swingFlow;
    }

    public void initializeGuy() {
        frame = new JFrame("flow");
        frame.getContentPane().setLayout(new GridLayout());
        populateFlow();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    private void populateFlow() {
        if (!swingFlow.hasNextCollector()) {
            endOfFlow();
            return;
        }
        CollectorIfc nextCollector = swingFlow.getNextCollector();
        flowPerson = (PersonVo) nextCollector.getModel();
        propertyEditor = new PropertyEditor(flowPerson, "Age");
        frame.getContentPane().removeAll();
        final Button button = new Button("next");
        button.addActionListener(this);
        JPanel panel = new JPanel();
        panel.setAlignmentX(LEFT_ALIGNMENT);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel.add(propertyEditor);
        panel.add(button);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }

       public void actionPerformed(ActionEvent e) {
        final int i = propertyEditor.getValue();
        flowPerson.setAge(i);
        laMalinche.updateConstraintStatus();
        if (!constraintResult.getInvalidConstraints().isEmpty()) {
            JOptionPane.showMessageDialog(frame, getConstraintString());
        } else {
            populateFlow();
        }
    }

      public void constraintViolated(Object validationResult) {
        constraintResult = ((ConstraintResult) validationResult);
    }

    public void bussinessRelevantPropertyChanged(Map<BusinessRuleType, Set<String>> proertyMap) {
        Set<String> stringSet = proertyMap.get(BusinessRuleType.constraint);
        if (!stringSet.isEmpty()) {
            System.out.print(stringSet);
        }
    }

     private void endOfFlow() {
        // todo
        frame.getContentPane().removeAll();
        frame.getContentPane().add(new JLabel("FIN"));
        frame.pack();
        frame.setVisible(true);
      }

      String getConstraintString() {
        final Set<ProductionRule> constraints = constraintResult.getInvalidConstraints();
        StringBuffer buf = new StringBuffer();
        for (ProductionRule rule : constraints) {
            buf.append("there is a constraint you passed: ");
            buf.append(rule.getName() + "\n");
        }
        return buf.toString();
    }
}
